package com.FindMyStuff.Activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

import com.FindMyStuff.Model.DataAccessor;
import com.FindMyStuff.Model.DataBaseHelper;
import com.FindMyStuff.Model.SessionManager;
import com.FindMyStuff.Model.User;

public class AdminUnlockActivity extends Activity {

	private SessionManager sm;
	private DataBaseHelper dbh;
	private DataAccessor da;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_admin_unlock);
		sm = SessionManager.getInstance();
		dbh = new DataBaseHelper(this);
		da = new DataAccessor(dbh);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_admin, menu);
		return true;
	}
	
	/**
	 * Attempts to unlock the user specified
	 * by the text box
	 * @param View
	 */
	public void unlockUser(View View){
		User user = getUser();
		//store success/fail in this boolean
		boolean success = user.exist();
		if(success){
			da.unlockUser(user);
		}
		
		Dialog unlockAttempt = createUnlockDialog(success);
		unlockAttempt.show();
	}
	
	/**
	 * Creates a Dialog to display a message after attempting 
	 * to unlock a User.
	 * @param success boolean showing if the operation was successful
	 * @return Dialog The Dialog object containing the message
	 */
	public Dialog createUnlockDialog(boolean success) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);

	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
	    if(success){
	    	builder.setMessage("User has been successfully unlocked.");
	    }
	    else{
	    	builder.setMessage("Failed to unlock user.");
	    }
	    builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   
	               }
	           });
	    return builder.create();
	}
	
	/**
	 * Attempts to lock the user specified
	 * by the text box
	 * @param View
	 */
	public void lockUser(View View){
		User user = getUser();
		if(user.getEmail().equals("admin")){
			Dialog admin = createAdminDialog();
			admin.show();
		}
		else if(user.getEmail().equals(sm.getCurrentUser().getEmail())){
			Dialog currentError = createCurrentDialog();
			currentError.show();
		}
		else{
			//store success/fail in this boolean
			boolean success = user.exist();
			if(success){
				da.lockUser(user);
			}
			
			Dialog lockAttempt = createLockDialog(success);
			lockAttempt.show();
		}
	}
	
	/**
	 * Creates a Dialog to display a message after attempting 
	 * to lock a User.
	 * @param success boolean showing if the operation was successful
	 * @return Dialog The Dialog object containing the message
	 */
	public Dialog createLockDialog(boolean success) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);

	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
	    if(success){
	    	builder.setMessage("User has been successfully locked.");
	    }
	    else{
	    	builder.setMessage("Failed to lock user.");
	    }
	    builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   
	               }
	           });
	    return builder.create();
	}
	
	/**
	 * Attempts to remove the user specified
	 * by the text box
	 * @param View
	 */
	public void removeUser(View View){
		User user = getUser();
		//prevent removal of default admin
		if(user.getEmail().equals("admin")){
			Dialog admin = createAdminDialog();
			admin.show();
		}
		else if(user.getEmail().equals(sm.getCurrentUser().getEmail())){
			Dialog currentError = createCurrentDialog();
			currentError.show();
		}
		else{
			//store success/fail in this boolean
			boolean success = user.exist();
			DataBaseHelper dbh = new DataBaseHelper(this);
			DataAccessor da = new DataAccessor(dbh);
			if(success){
				success = da.removeUser(user.getEmail());
			}
			
			Dialog removeAttempt = createRemoveDialog(success);
			removeAttempt.show();
		}
	}
	
	/**
	 * Creates a Dialog to display a message after attempting 
	 * to remove a User.
	 * @param success boolean showing if the operation was successful
	 * @return Dialog The Dialog object containing the message
	 */
	public Dialog createRemoveDialog(boolean success) {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);

	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
	    if(success){
	    	builder.setMessage("User has been successfully removed.");
	    }
	    else{
	    	builder.setMessage("Failed to remove user.");
	    }
	    builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   
	               }
	           });
	    return builder.create();
	}
	
	/**
	 * Creates a Dialog to display a message after attempting 
	 * to remove or lock the default admin account
	 * @return Dialog The Dialog object containing the message
	 */
	public Dialog createAdminDialog() {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);

	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
    	builder.setMessage("Cannot modify the default admin.");
	    builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   
	               }
	           });
	    return builder.create();
	}
	
	/**
	 * Creates a Dialog to display a message after attempting 
	 * to remove or lock the current account
	 * @return Dialog The Dialog object containing the message
	 */
	public Dialog createCurrentDialog() {
	    AlertDialog.Builder builder = new AlertDialog.Builder(this);

	    // Inflate and set the layout for the dialog
	    // Pass null as the parent view because its going in the dialog layout
    	builder.setMessage("Cannot modify the current user.");
	    builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
	               @Override
	               public void onClick(DialogInterface dialog, int id) {
	            	   
	               }
	           });
	    return builder.create();
	}
	
	/**
	 * Gets the user for the admin to unlock
	 * @return User the user to be unlocked
	 */
	private User getUser(){
		EditText emailText = (EditText) findViewById(R.id.editText1);
		String email = emailText.getText().toString();
		
		//get user to unlock
		DataBaseHelper dbh = new DataBaseHelper(this);
		DataAccessor da = new DataAccessor(dbh);
		return da.getUserByEmail(email);
		
	}

}
